package com.study.lihan.springmvc.service;

import com.study.lihan.springmvc.bean.MyException;
import com.study.lihan.springmvc.mapper.MainThreadMapper;
import com.study.lihan.springmvc.mapper.ZiThreadMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/*
* 两个方法在不同的事务中
* */
@Service
public class TxService2 {


    @Autowired
    public MainThreadMapper mainThreadMapper;
    @Autowired
    public ZiThreadMapper ziThreadMapper;
    @Autowired
    @Lazy
    private TxService2 txService2;

    @Transactional(rollbackFor = Exception.class)
    public void threadTestTransactional() {
        mainThreadMapper.updateById(1,"main");
        try{
         txService2.hello();
        }catch (Exception e){
            System.out.println("捕获了异常");
        }
    }
    @Transactional(rollbackFor = Exception.class)
    public void hello(){
        ziThreadMapper.updateById(1,"zi");

    }

    public void yuner(){
        ziThreadMapper.updateById(1,"yuner");
        throw new MyException("抛出异常");
    }
}
